//
//  ZGLRechargeController.m
//  ZeegingLottery
//
//  Created by 李曌 on 2018/8/9.
//  Copyright © 2018年 蒂埃里. All rights reserved.
//

#import "ZGLRechargeController.h"
#import "ZGLRechargeHeaderView.h"
#import "ZGLOrderPayCell.h"
#import "ZGLRecordDetailController.h"

@interface ZGLRechargeController ()<UITableViewDelegate,UITableViewDataSource,UIScrollViewDelegate>

@property (nonatomic, strong) UIScrollView *BgScrollerView;

@property (nonatomic, strong) ZGLRechargeHeaderView *headerView;

@property (nonatomic, strong) UITableView *modeTableView;
@property (nonatomic, strong) UILabel *modeTipLable; //方式提示
@property (nonatomic, strong) UIButton *rechargeBtn; //充值按钮
@property (nonatomic, strong)NSArray *payTypeNameArr;
@property (nonatomic, strong)NSArray *payTypeImageArr;
@property (nonatomic, assign)NSIndexPath *lastSelected;  //上一次选中的
@property (nonatomic, copy) NSString *chatgeMoney;  //充值金额

@property (nonatomic, copy) NSString *wechat_pay;
@property (nonatomic, copy) NSString *ali_pay;

@end

@implementation ZGLRechargeController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"充值";
    self.view.backgroundColor = ViewBackgroundColor;
    
    [self.view addSubview:self.BgScrollerView];
    [self setupUI];

    _payTypeNameArr = @[@"微信支付",@"支付宝",@"微信转账"];
    _payTypeImageArr = @[@"icon_charge_wechat",@"icon_charge_alipay",@"icon_charge_phonebill",];
    
    
    //取支付支持状态
    NSString *path = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)lastObject];
    NSString *fileName = [path stringByAppendingPathComponent:@"Paylist.plist"];
    NSDictionary *paymentList = [NSDictionary dictionaryWithContentsOfFile:fileName];
    self.wechat_pay = paymentList[@"wechat_pay"];
    self.ali_pay = paymentList[@"ali_pay"];
    
}

#pragma mark - meoth

//收键盘
- (void)hideKetBoadr
{
    [self.view endEditing:YES];
}

//马上充值
- (void)chagrgeMoney:(UIButton *)sneder
{
    DLog(@"马上充值");
    if ([self.chatgeMoney integerValue] != 0) {
        
        if (self.lastSelected) {
            
            
            NSString *chargeLabel = sZGLUserInfoModel.mobile;
            NSMutableDictionary *parame = [NSMutableDictionary dictionary];
            [parame setObject:@"money" forKey:self.chatgeMoney];
            if (self.lastSelected.row == 0) {
                [parame setObject:@"payment" forKey:@"weChat_Pay"];
                
                
            }else if (self.lastSelected.row == 1){
                [parame setObject:@"payment" forKey:@"Ali_Pay"];
                
                
            }else if (self.lastSelected.row == 2){
                [parame setObject:@"payment" forKey:@"WeChat_Transfer"];
                
                //微信转账
                NSString *transferPageUrl = [NSString stringWithFormat:@"http://api.bybrain.cn/deposit.html?amount=%@token=%@",self.chatgeMoney,sZGLUserInfoModel.token];
                
                WKWebViewController *transferPage = [[WKWebViewController alloc]init];
                transferPage.isNavHidden = YES;
                [transferPage loadWebURLSring:transferPageUrl];
                
                [self.navigationController pushViewController:transferPage animated:YES];
            }
            //埋点
            [TalkingData trackEvent:ZGLChargingBtnEventID label:chargeLabel parameters:parame];
            
        }else{
           
            [EasyTextView showText:@"请选择支付方式"];
        }
        
    }else{
        
        DLog(@"请输入充值金额");
        [EasyTextView showText:@"请输入充值金额"];
        
    }
    
}

#pragma mark - getter

- (void)setupUI
{
    [_BgScrollerView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.edges.equalTo(self.view);
    }];
    
    UIView *contentView = [[UIView alloc]init];
    contentView.backgroundColor = ViewBackgroundColor;
    [self.BgScrollerView addSubview:contentView];
    [contentView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.edges.equalTo(self.BgScrollerView);
        make.width.equalTo(self.BgScrollerView);
        
    }];
    
    [contentView addSubview:self.headerView];
    [contentView addSubview:self.rechargeBtn];
    [contentView addSubview:self.modeTableView];
    
    [_headerView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.equalTo(_BgScrollerView);
        make.left.equalTo(self.view);
        make.right.equalTo(self.view);
        make.height.equalTo(@(230));
        
    }];

    UIView *modeBgView = [[UIView alloc]init];
    modeBgView.backgroundColor = [UIColor whiteColor];
    [contentView addSubview:modeBgView];
    [modeBgView mas_makeConstraints:^(MASConstraintMaker *make) {
       
        make.top.equalTo(_headerView.mas_bottom).offset(10);
        make.left.equalTo(self.view);
        make.right.equalTo(self.view);
    }];
    
    [modeBgView addSubview:self.modeTipLable];
    [_modeTipLable mas_makeConstraints:^(MASConstraintMaker *make) {

        make.top.equalTo(modeBgView).offset(10);
        make.left.equalTo(modeBgView).offset(20);

    }];

    UIView *line1 = [[UIView alloc]init];
    line1.backgroundColor = BorderColor;
    [modeBgView addSubview:line1];
    [line1 mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.equalTo(_modeTipLable.mas_bottom).offset(9);
        make.left.equalTo(_modeTipLable);
        make.right.equalTo(modeBgView);
        make.height.equalTo(@(kBorderWidth));
    }];
    
    [modeBgView addSubview:self.modeTableView];
    [_modeTableView mas_makeConstraints:^(MASConstraintMaker *make) {

        make.top.equalTo(_modeTipLable.mas_bottom).offset(10);
        make.left.equalTo(modeBgView);
        make.right.equalTo(modeBgView);
        make.height.equalTo(@(150));
        make.bottom.equalTo(modeBgView);
    }];
    
    [_rechargeBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.equalTo(modeBgView.mas_bottom).offset(40);
        make.left.equalTo(modeBgView).offset(20);
        make.right.equalTo(modeBgView).offset(-20);
        make.height.equalTo(@(40));
    }];
    
    [contentView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.bottom.equalTo(_rechargeBtn.mas_bottom).offset(40);
    }];
    
}

- (UIButton *)rechargeBtn
{
    if (!_rechargeBtn) {
        _rechargeBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        _rechargeBtn.layer.backgroundColor = globalRed.CGColor;
        _rechargeBtn.layer.cornerRadius = 3;
        _rechargeBtn.layer.masksToBounds = YES;
        [_rechargeBtn setTitle:@"马上充值" forState:UIControlStateNormal];
        [_rechargeBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [_rechargeBtn addTarget:self action:@selector(chagrgeMoney:) forControlEvents:UIControlEventTouchUpInside];
    }
    
    return _rechargeBtn;
}

- (ZGLRechargeHeaderView *)headerView
{
    if (!_headerView) {
//        _headerView = [[ZGLRechargeHeaderView alloc]initWithFrame:CGRectMake(0, 0, UI_SCREEN_WIDTH, 230)];
        _headerView = [[ZGLRechargeHeaderView alloc]initWithFrame:CGRectZero];
        WS(weakself)
        _headerView.moneyBlock = ^(NSString *moneyStr) {
            
            weakSelf.chatgeMoney = moneyStr;
            
            [TalkingData trackEvent:ZGLChargingBtnEventID label:[NSString stringWithFormat:@"充值金额:%@",moneyStr]];
            DLog(@"moneyBlock--%@",weakSelf.chatgeMoney);
            
        };
    }
    return _headerView;
}

- (UILabel *)modeTipLable
{
    if (!_modeTipLable) {
        _modeTipLable = [[UILabel alloc]init];
        _modeTipLable.text = @"请选择支付方式";
        _modeTipLable.font = [UIFont systemFontOfSize:14];
        _modeTipLable.layer.backgroundColor = [UIColor whiteColor].CGColor;
    }
    return _modeTipLable;
}

- (UITableView *)modeTableView
{
    if (!_modeTableView) {
//        _modeTableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 240, UI_SCREEN_WIDTH, 200)];
        _modeTableView = [[UITableView alloc]initWithFrame:CGRectZero style:UITableViewStylePlain];
        _modeTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _modeTableView.dataSource = self;
        _modeTableView.delegate = self;
    }
    
    return _modeTableView;
}

- (UIScrollView *)BgScrollerView
{
    if (!_BgScrollerView) {
//        _BgScrollerView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, UI_SCREEN_WIDTH, UI_SCREEN_HEIGHT)];
        _BgScrollerView = [[UIScrollView alloc]init];
        [_BgScrollerView setShowsVerticalScrollIndicator:NO];
        _BgScrollerView.backgroundColor = ViewBackgroundColor;
        UITapGestureRecognizer *tapGeature = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(hideKetBoadr)];
        tapGeature.cancelsTouchesInView = NO;//设置成NO表示当前控件响应后会传播到其他控件上，默认为YES。
        [_BgScrollerView addGestureRecognizer:tapGeature];
        
    }
    
    
    return _BgScrollerView;
}

#pragma maek - UITableViewDelegate,UITableViewDataSource

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.payTypeNameArr.count;
    
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    ZGLOrderPayCell *cell = [ZGLOrderPayCell orderPayCellWith:tableView];
    cell.ali_pay = self.ali_pay;
    cell.weChat_pay = self.wechat_pay;
    [cell setNameWith:_payTypeNameArr[indexPath.row] Icon:_payTypeImageArr[indexPath.row]];
    
    if ([self.lastSelected isEqual:indexPath]) {
        [cell.selectedImage setHidden:NO];
    }else{
        [cell.selectedImage setHidden:YES];
    }
    
    return cell;
    
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 50;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row == 0 && [self.wechat_pay integerValue] == 0) {
        DLog(@"微信充值暂不支持");
        //
        [TalkingData trackEvent:ZGLChargingBtnEventID label:@"weChat_Pay"];
        
    }else if (indexPath.row == 1 && [self.ali_pay integerValue] == 0){
        DLog(@"支付宝充值暂不支持");
        //
        [TalkingData trackEvent:ZGLChargingBtnEventID label:@"Ali_Pay"];
        
        
    }else {
        
        if (self.lastSelected) {
            ZGLOrderPayCell *cell = [tableView cellForRowAtIndexPath:self.lastSelected];
            [cell.selectedImage setHidden:YES];
        }
        
        ZGLOrderPayCell *cell = [tableView cellForRowAtIndexPath:indexPath];
        [cell.selectedImage setHidden:NO];
        
        self.lastSelected = indexPath;
        
        [TalkingData trackEvent:ZGLChargingBtnEventID label:@"WeChat_Transfer"];
    }
    
    
//    if (self.lastSelected.row == 0) {
//    }else if (self.lastSelected.row == 1){
//    }else if (self.lastSelected.row == 2){
//    }
    
    DLog(@"已选择-%ld--%@",self.lastSelected.row,_payTypeNameArr[indexPath.row]);
    
    
    
    
    
    
    
    
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
